package com.bjx.flow.mmx.task;


import com.bjx.flow.mmx.bean.MmxFlowData;
import com.bjx.flow.mmx.bean.MmxLineData;
import com.bjx.flow.mmx.bean.MmxNodeData;
import com.bjx.flow.mmx.constant.MmxNodeConstant;
import com.bjx.flow.mmx.domain.bo.MmxNodeFlowBO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @desc: 任务处理抽象类
 * @author: liushuai
 * @date: 2023/1/31 15:15
 */
public abstract class MmxAbstractNodeFlowDispose {

    public static final List<String> TRANSIT_NODE = new ArrayList<>();

    static {
        TRANSIT_NODE.add(MmxNodeConstant.EXCLUSIVE_GATEWAY);
        TRANSIT_NODE.add(MmxNodeConstant.INCLUSIVE_GATEWAY);
        TRANSIT_NODE.add(MmxNodeConstant.PARALLEL_GATEWAY);
    }


    /**
     * 流程流转执行方法
     * @param flowObj
     * @param currentNode
     * @param variables
     * @return: java.util.List<com.mmx.domain.bean.NodeData>
     * @Author: liushuai
     * @Date: 2023/1/31 17:26
     */
    public abstract MmxNodeFlowBO execute(MmxFlowData flowObj, MmxNodeData currentNode, Map<String, Object> variables);

    protected List<MmxNodeData> getTargerNode(MmxFlowData flowObj, MmxNodeData currentNode){
        List<MmxLineData> edges = flowObj.getEdges();
        List<MmxNodeData> nodeDatas = new ArrayList();
        List<MmxLineData> targetLines = new ArrayList();
        for(MmxLineData lineData: edges) {
            if(currentNode.getId().equals(lineData.getSourceNodeId())){
                targetLines.add(lineData);
            }
        }
        List<MmxNodeData> nodes = flowObj.getNodes();
        for(MmxNodeData nodeData: nodes) {
            for(MmxLineData lineData: targetLines) {
                if(lineData.getTargetNodeId().equals(nodeData.getId())){
                    nodeData.setCondition(lineData.getProperties().getCondition());
                    nodeDatas.add(nodeData);
                }
            }

        }
        return  nodeDatas;
    }

}
